package com.swy.shopgroup.controller;

import com.swy.shopgroup.ResultUtils;
import com.swy.shopgroup.UploadFilePathConfig;
import com.swy.shopgroup.dao.ShopUserMapper;
import com.swy.shopgroup.entity.Result;
import com.swy.shopgroup.entity.ShopUser;
import com.swy.shopgroup.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/User")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private ShopUserMapper userMapper;

    @RequestMapping(value = "/login")
    @ResponseBody
    public Result login(String name, String pwd) {
        return userService.login(name, pwd);
    }

    @RequestMapping(value = "/updateUser")
    @ResponseBody
    public Result update(ShopUser user) {
        return userService.updateUser(user);
    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(@RequestParam("file") MultipartFile file, ShopUser user) throws IOException {
//        String path = "E:/ideaApplication/newwind/windpower/shopGroup/src/main/java/com/swy/shopgroup/upload/";
        ShopUser user1 = userMapper.selectByName(user.getName());
        if (user1 != null) {
            return ResultUtils.err("该用户名已注册");
        }
        String path = UploadFilePathConfig.uploadPath;
        // 上传图片
        if (file != null && !file.isEmpty()) {
            // 获取文件的名称
            String oldname = file.getOriginalFilename();
            // 获取文件的后缀名
            String sux = oldname.substring(oldname.lastIndexOf("."));
            // 自动生成文件名
            String newname = UUID.randomUUID().toString() + sux;
            // 获取文件的上传路径（物理路径）
            String upload = path + newname;
            System.out.println(upload);
            // 上传图片
            file.transferTo(new File(upload));
            user.setAvatar(newname);
        }
        return userService.insertUser(user);
    }

    @RequestMapping("/changeIcon")
    @ResponseBody
    public Result changeIcon(@RequestParam("file") MultipartFile file, ShopUser user) throws IOException {
//        String path = "E:/moneyapplication/course/src/main/java/com/cum/course/upload/";
        String path = UploadFilePathConfig.uploadPath;
        // 上传图片
        if (file != null && !file.isEmpty()) {
            // 获取文件的名称
            String oldname = file.getOriginalFilename();
            // 获取文件的后缀名
            String sux = oldname.substring(oldname.lastIndexOf("."));
            // 自动生成文件名
            String newname = UUID.randomUUID().toString() + sux;
            // 获取文件的上传路径（物理路径）
            String upload = path + newname;
            System.out.println(upload);
            // 上传图片
            file.transferTo(new File(upload));
            user.setAvatar(newname);
        }
        return userService.updateUser(user);
    }
}
